
/**
 * 提交保存
 */
function userFormSave() {
    let userForm = $("#userForm").serializeObject();
    userForm.createTime = commonUtil.getNowTime();
    let roleIdList = [];
    for (let check of tree.getChecked('userAuthorityTree')[0].children) {
        let roled = {
            roleId: check.id
        }
        roleIdList.push(roled);
    }
    userForm.roles = roleIdList;
    $.DPost(ctx + "/user/c/saveOrUpdate", userForm, headers,function (data) {
        if (data.code === '00000') {
            layer.msg("保存成功", {icon: 1, time: 2000}, function () {
                // 关闭当前页面
                parent.layer.close(parent.layer.getFrameIndex(window.name));
                // 刷新父页面table(连同查询条件)：
                parent.layui.table.reload('userTable');
            });
        }else{
            layer.msg("保存失败", {icon: 1, time: 2000}, function () {
                // 关闭当前页面
                parent.layer.close(parent.layer.getFrameIndex(window.name));
                // 刷新父页面table(连同查询条件)：
                parent.layui.table.reload('userTable');
            });
        }

    });
}

/**
 * 重置密码
 */
function resetPassword() {
    let userForm = $("#userForm").serializeObject();
    if (userForm.userId === "") {
        layer.msg("新增用户无需点这个按钮，如需重置请先选择需要重置的用户！", {icon: 2, time: 2000}, function () {
        });
        return;
    }
    $.DPost(ctx + "/user/c/resetPassword", userForm, function (data) {
        if (data.flag) {
            layer.msg("密码重置成功，请尽快登录系统修改密码！", {icon: 1, time: 2000}, function () {
            });
        }
    });
}
/**
 * 加载用户菜单
 */
function loadMenuTree() {
    let userForm = $("#userForm").serializeObject();
    let data  ={
        'userId': userForm.userId
    }
    //获取菜单数据
    $.DPost(ctx + "/permission/c/findUserMenuByUserId", data, headers, function (data) {
        //数据说明：id对应id，title对应menuName，href对应menuPath
        let treeData = commonUtil.updateKeyForLayuiTree(data.data.permissionHave);
        // 存储所有菜单信息
        localStorage.setItem("permissionAll", JSON.stringify(data.data.permissionAll));
        //回显用户菜单
        treeData = commonUtil.checkedForLayuiTree(treeData, JSON.stringify(data.data.permissionAll));
        //开启节点操作图标
        tree.render({
            elem: '#userMenuTree'
            , id: 'userMenuTree'
            , data: [{
                title: '系统菜单根节点'
                , href: "/"
                , id: 0
                , spread: true
                , children: treeData
            }]
            , showCheckbox: true
        });
    });
}

/**
 * 加载用户权限
 */
function loadAuthorityTree() {
    let userForm = $("#userForm").serializeObject();
    let data  = {
        'userId': userForm.userId
    }
    //获取菜单数据
    $.DPost(ctx + "/role/c/findUserRoleByUserId", data, headers, function (data) {
        //数据说明：id对应id，title对应menuName，href对应menuPath
        let treeData = [];
        let userTreeString = JSON.stringify(data.data.roleHave);
        for (let role of data.data.roleAll) {
            let tree = {
                title: role.roleName
                , id: role.roleId
                , spread: true
            };
            //回显用户权限
            if(userTreeString.search(role.roleId) !== -1){
                tree.checked = true;
            }
            treeData.push(tree);
        }
        //开启节点操作图标
        roleTree = tree.render({
            elem: '#userAuthorityTree'
            , id: 'userAuthorityTree'
            , data: [{
                title: '系统权限根节点'
                , href: "/"
                , id: 0
                , spread: true
                , children: treeData
            }]
            , showCheckbox: true
            // , oncheck: roleChangeMenu
        });
    });
}

/**
 * 角色改变刷新菜单权限
 */
function roleChangeMenu() {
    let userForm = $("#userForm").serializeObject();
    // 获得角色选中的节点
    let roleIdList = [];
    this.elem.find('.layui-form-checked').each(function(){
        roleIdList.push($(this).prev()[0].value)
    });
    // let roleList = [];
    // for (let check of roleList) {
    //     roleIdList.push(check.id);
    // }
    let data = {
        userId: userForm.userId,
        roleIdList: roleIdList.join(",")
    };
    // 刷新菜单,根据角色ID查询对应的菜单权限
    $.DPost(ctx + "/permission/c/findMenuByRoleId", data, headers, function (data) {
        //数据说明：id对应id，title对应menuName，href对应menuPath
        let treeData = commonUtil.updateKeyForLayuiTree(data.data.permissionHave);
        //回显用户菜单
        treeData = commonUtil.checkedForLayuiTree(treeData, JSON.stringify(data.data.permissionAll));
        //开启节点操作图标
        tree.render({
            elem: '#userMenuTree'
            , id: 'userMenuTree'
            , data: [{
                title: '系统菜单根节点'
                , href: "/"
                , id: 0
                , spread: true
                , children: treeData
            }]
            , showCheckbox: true
        });
    });
}